const webpack = require('webpack')
const path = require('path')
const CopyWebpackPlugin = require('copy-webpack-plugin')

const cesiumSource = 'node_modules/mars3d-cesium/Build/Cesium/'
console.log(process.env.VUE_APP_BASE_API)
module.exports = {
    publicPath: './',
    assetsDir: './static',
    productionSourceMap: false,
    lintOnSave: false, // 是否开启eslint
    // 它支持webPack-dev-server的所有选项
    devServer: {
        // host: 'localhost', // 也可以直接写IP地址这样方便真机测试
        port: 3301, // 端口号
        disableHostCheck: true,
        https: false, // https:{type:Boolean}
        open: false, // 配置自动启动浏览器
        compress: true, // 开启压缩
        overlay: {
            warnings: false,
            errors: true
        }
        // proxy: {
        //     '/api/map': {
        //         target: 'http://59.110.54.18:9088', //接口域名
        //         changeOrigin: true, //是否跨域
        //         ws: true, //是否代理 websockets
        //         // secure: true, //是否https接口
        //         pathRewrite: {
        //             //路径重置
        //             '^/api/map': '/api/map'
        //         }
        //     },
        //     '/file/mapLayer': {
        //         target: 'http://59.110.54.18:9088', //接口域名
        //         changeOrigin: true, //是否跨域
        //         ws: true, //是否代理 websockets
        //         // secure: true, //是否https接口
        //         pathRewrite: {
        //             //路径重置
        //             '^/file/mapLayer': '/file/mapLayer'
        //         }
        //     }
        // }
    },

    configureWebpack: config => {
        let plugins = []
        if (process.env.NODE_ENV === 'production') {
            plugins = [
                new webpack.ProvidePlugin({
                    $: 'jquery',
                    jQuery: 'jquery',
                    jquery: 'jquery',
                    'window.jQuery': 'jquery'
                }),
                new webpack.DefinePlugin({
                    CESIUM_BASE_URL: JSON.stringify('static')
                }),
                new CopyWebpackPlugin([
                    {
                        from: path.join(cesiumSource, 'Workers'),
                        to: 'static/Workers'
                    }
                ]),
                new CopyWebpackPlugin([
                    {
                        from: path.join(cesiumSource, 'Assets'),
                        to: 'static/Assets'
                    }
                ]),
                new CopyWebpackPlugin([
                    {
                        from: path.join(cesiumSource, 'ThirdParty'),
                        to: 'static/ThirdParty'
                    }
                ]),
                new CopyWebpackPlugin([
                    {
                        from: path.join(cesiumSource, 'Widgets'),
                        to: 'static/Widgets'
                    }
                ])
            ]
        } else {
            plugins = [
                new webpack.ProvidePlugin({
                    $: 'jquery',
                    jQuery: 'jquery',
                    jquery: 'jquery',
                    'window.jQuery': 'jquery'
                }),
                new webpack.DefinePlugin({
                    CESIUM_BASE_URL: JSON.stringify('')
                }),
                new CopyWebpackPlugin([
                    {
                        from: path.join(cesiumSource, 'Workers'),
                        to: 'Workers'
                    }
                ]),
                new CopyWebpackPlugin([
                    {
                        from: path.join(cesiumSource, 'Assets'),
                        to: 'Assets'
                    }
                ]),
                new CopyWebpackPlugin([
                    {
                        from: path.join(cesiumSource, 'ThirdParty'),
                        to: 'ThirdParty'
                    }
                ]),
                new CopyWebpackPlugin([
                    {
                        from: path.join(cesiumSource, 'Widgets'),
                        to: 'Widgets'
                    }
                ])
            ]
        }
        return {
            module: {
                unknownContextCritical: false,
                rules: [
                    {
                        test: /\.js$/,
                        enforce: 'pre',
                        include: path.resolve(__dirname, 'node_modules/mars3d-cesium/Source'),
                        sideEffects: false
                    }
                ]
            },
            optimization: {
                usedExports: true,
                splitChunks: {
                    maxInitialRequests: Infinity,
                    minSize: 0,
                    maxSize: 250000,
                    cacheGroups: {
                        vendor: {
                            test: /[\\/]node_modules[\\/]/,
                            priority: -10,
                            chunks: 'all',
                            name(module) {
                                const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
                                return `npm.${packageName.replace('@', '')}`
                            }
                        },
                        commons: {
                            name: 'Cesium',
                            test: /[\\/]node_modules[\\/]mars3d-cesium[\\/]Build[\\/]Cesium/,
                            priority: 10,
                            chunks: 'all'
                        }
                    }
                }
            },
            output: {
                sourcePrefix: ' '
            },
            amd: {
                toUrlUndefined: true
            },
            resolve: {
                alias: {
                    vue$: 'vue/dist/vue.esm.js',
                    '@': path.resolve('src')
                }
            },
            node: {
                fs: 'empty',
                Buffer: false,
                http: 'empty',
                https: 'empty',
                zlib: 'empty'
            },
            plugins: plugins
        }
    }
}
